<?php

namespace app\model\v1\repair;

use app\model\user\User;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;

class SurveyorService extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 勘察员管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getSurveyorList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(add_time) as add_time,
            case when status=1 then '启用' 
                when status=0 then '禁用' 
                end as status_text,
            case when type=0 then '平台' 
                when type=1 then '门店' 
                when type=2 then '供应商' 
                end as type_text 
            ")
            ->where(function ($query) use ($param){
                $query->where("is_del", 0);
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('add_time', 'between', [strtotime($wheretime[0]),  strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("nickname",'like', '%'.$param["word"].'%');
                if(!empty($param["phone"])) $query->where("phone",'like', '%'.$param["phone"].'%');
                if(!empty($param["type"])){
                    if($param["type"] == 999) $param["type"] = 0;
                    $query->where("type", $param["type"]);
                }
            })
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->where(function ($query) use ($param){
                $query->where("is_del", 0);
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('add_time', 'between', [strtotime($wheretime[0]),  strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("nickname",'like', '%'.$param["word"].'%');
                if(!empty($param["phone"])) $query->where("phone",'like', '%'.$param["phone"].'%');
                if(!empty($param["type"])){
                    if($param["type"] == 999) $param["type"] = 0;
                    $query->where("type", $param["type"]);
                }
            })
            ->count();

        return $data;
    }


    /**
     * 勘察员管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateSurveyor($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["type"]))          $update["type"] = $param["type"];
        if(!empty($param["relation_id"]))   $update["relation_id"] = $param["relation_id"];
        if(!empty($param["avatar"]))        $update["avatar"] = $param["avatar"];
        if(!empty($param["nickname"]))      $update["nickname"] = $param["nickname"];
        if(!empty($param["phone"])){
            $userid = User::where("phone", $param["phone"])->value("uid") ?? 0;
            if(empty($userid)) Throw new Exception("系统内未找到此手机号用户！");

            $update["uid"] = $userid;
            $update["phone"] = $param["phone"];
        }
        if(!empty($param["polling"]))      $update["polling"] = $param["polling"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 勘察员管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addConstruction($param = [])
    {
        $add = [
            "type" => $param["type"] ?? 0,
            "relation_id" => $param["relation_id"] ?? 0,
            "avatar" => $param["avatar"] ?? '',
            "nickname" => $param["nickname"] ?? '',
            "phone" => $param["phone"] ?? '',
            "polling" => $param["polling"] ?? 0,
            "add_time"   => time(),
        ];
        $userid = User::where("phone", $param["phone"])->value("uid") ?? 0;

        if(empty($userid)) Throw new Exception("系统内未找到此手机号用户！");
        $add["uid"] = $userid;
        return self::insert($add);
    }




    /**
     * 勘察员管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableSurveyor($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 0;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }




}